//
//  NearByViewController.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/18.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "NearByViewController.h"
//地图
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
//#import <AMapLocationKit/AMapLocationKit.h>
#import <MapKit/MapKit.h>

#import "NearbyTableViewCell.h"
#import "ShopDetailsViewController.h"

#import "ShopListModel.h"

@interface NearByViewController ()<MAMapViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)MAMapView * mapView;
//@property (nonatomic, strong)AMapLocationManager *locationManager;
@property (nonatomic, weak)UITableView * tableView;
//@property (nonatomic, strong)CLLocation * currentLocation;

@property (nonatomic, strong)NSArray<ShopListModel *> * shopModelArray;

@end

@implementation NearByViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
    
    [self loadData];
    [self setupUI];
}

//- (void)setCurrentLocation:(CLLocation *)currentLocation{
//
//    _currentLocation = currentLocation;
//
//    double lat = currentLocation.coordinate.latitude;
//    double lon = currentLocation.coordinate.longitude;
//
//    [[ZRNetworkTool sharedTool] getShopListResultWithLat:lat Withlon:lon WithBlock:^(BOOL success, id responseData) {
//
//        if (success) {
//
//            self.shopModelArray = [NSArray yy_modelArrayWithClass:[ShopListModel class] json:responseData[@"data"]];
//        }
//
//
//    }];
//}


- (void)loadData{
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    [[ZRNetworkTool sharedTool] getShopListResultWithLat:userLat Withlon:userLon WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.shopModelArray = [NSArray yy_modelArrayWithClass:[ShopListModel class] json:responseData[@"data"]];
        }
        
        
    }];

}


- (void)setShopModelArray:(NSArray<ShopListModel *> *)shopModelArray{
    
    _shopModelArray = shopModelArray;
    
    [self.tableView reloadData];
}

- (void)setupUI {
   
    ///初始化地图
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    self.mapView.showsCompass = NO;
    self.mapView.showsScale = NO;
    [self.mapView setZoomLevel:14 animated:YES];
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    ///把地图添加至view
    [self.view addSubview:self.mapView];
    
//    self.locationManager = [[AMapLocationManager alloc] init];
//    self.locationManager.delegate = self;
//    [self.locationManager startUpdatingLocation];
//
//    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];    // 带逆地理信息的一次定位（返回坐标和地址信息）
//    self.locationManager.locationTimeout =2;    //   定位超时时间，最低2s，此处设置为2s
//    self.locationManager.reGeocodeTimeout = 2;
    
    
    int yValue = kScreenH - 243 * SCREENH6 - kTabBarHeight;
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, yValue, kScreenW, 240 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.rowHeight = 124 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;

    self.tableView = tableView;
    
    [self.tableView registerClass:[NearbyTableViewCell class] forCellReuseIdentifier:@"NearbyListCell"];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.shopModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearbyListCell" forIndexPath:indexPath];
    
    cell.model = self.shopModelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    ShopDetailsViewController * detailVC = [ShopDetailsViewController new];
    
    ShopListModel * model = self.shopModelArray[indexPath.row];
    
    detailVC.model = model;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    ShowBar;
}



@end
